import numpy as np


# 用于计算 高斯-勒让德点
def load_GLP(NumGLP):
    """计算高斯-勒让德点和权重

    Args:
        NumGLP (_type_): 高斯点个数

    Raises:
        ValueError: _description_

    Returns:
        _type_: weight, lambda_
    """
    if NumGLP == 5:
        weight = np.array(
            [
                0.2369268850561890875142640,
                0.4786286704993664680412915,
                0.5688888888888888888888889,
                0.4786286704993664680412915,
                0.2369268850561890875142640,
            ]
        )
        
        lambda_ = np.array(
            [
                -0.9061798459386639927976269,
                -0.5384693101056830910363144,
                0.0,
                0.5384693101056830910363144,
                0.9061798459386639927976269,
            ]
        )

    else:
        raise ValueError("Currently only supports NumGLP = 5")
    
    return weight, lambda_
    

def GaussIntegral(f, a, b, NumGLP=5):
    """在区间[a,b]上用NumGLP个Gauss点计算积分"""
    weight, lambda_ = load_GLP(NumGLP)
    integral = 0.0
    for i in range(NumGLP):
        x = 0.5 * (b - a) * lambda_[i] + 0.5 * (a + b)  # 变换到物理区间
        integral += f(x) * weight[i]
    integral *= 0.5 * (b - a)
    return integral